﻿namespace RuoVea.Fams.Server.Service;

using Microsoft.AspNetCore.Mvc;
using RuoVea.Fams.Server.Dto;

[ApiExplorerSettings(GroupName = "fams")]
public partial class RecordsService : IApplicationService
{
    /// <summary>
    /// 
    /// </summary>
    private readonly SugarRepository<FamsRecords> _thisRepository;
    private readonly ICurrentUser _currentUser;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="thisRepository"></param>
    /// <param name="currentUser"></param>
    public RecordsService(SugarRepository<FamsRecords> thisRepository, ICurrentUser currentUser)
    {
        _thisRepository = thisRepository;
        _currentUser = currentUser;
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns> 
    public async Task<RestfulResult<PageResult<FamsRecords>>> GetPagesAsync(RecordsPageParam? data)
    {
        var restful = new RestfulResult<PageResult<FamsRecords>>() { Code = CodeStatus.OK };
        data ??= new RecordsPageParam { PageNo = 1, PageSize = 10 };
        var query = await _thisRepository.AsQueryable()
            //.Where(u => u.Creator == _currentUser.UserId)
            .ToPageAsync(data.PageNo, data.PageSize);

        restful.Data = query;
        return restful;
    }

    /// <summary>
    /// 所有
    /// </summary>
    /// <returns></returns>
    public async Task<RestfulResult<PageResult<FamsRecords>>> GetlistAsync()
    {
        var restful = new RestfulResult<PageResult<FamsRecords>>() { Code = CodeStatus.OK };
        PageParam data = new PageParam { PageNo = 1, PageSize = 100 };
        var query = await _thisRepository.AsQueryable()
             //.Where(u => u.IsDisable == YesOrNot.N)
             //.Where(u => u.Creator == _currentUser.UserId)
             .OrderByDescending(u => u.Date)
             .Select(u => new FamsRecords
             {
                 Id = u.Id,
                 AssetId = u.AssetId,
                 Order = u.Order,
                 Date = u.Date
             })
            .ToPageAsync(data.PageNo, data.PageSize);
        restful.Data = query;
        return restful;
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    public async Task<RestfulResult> AddAsync(FamsRecords data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        var isExist = await _thisRepository.IsAnyAsync(u => u.Order == data.Order && u.Creator == _currentUser.UserId);
        if (isExist)
        {
            restful.Message = "维修单号重复";
            return restful;
        }
        data.Creator = _currentUser.UserId;
        bool result = await _thisRepository.InsertAsync(data);
        if (result)
        {
            restful.Data = result;
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Message = "添加失败";
            return restful;
        }
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public async Task<RestfulResult> UpdateAsync(FamsRecords data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        var isExist = await _thisRepository.IsAnyAsync(u => u.Id == data.Id && u.Creator == _currentUser.UserId);
        if (!isExist)
        {
            restful.Message = "记录不存在";
            return restful;
        }
        bool result = await _thisRepository.UpdateAsync(data, true, true, "维修记录修改");
        if (result)
        {
            restful.Data = result;
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Message = "修改失败";
            return restful;
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns> 
    public async Task<RestfulResult> DeleteAsync([FromBody] EntityBaseId data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };

        if (data.Id == 0)
        {
            restful.Message = "删除失败";
            return restful;
        }
        bool isDelete = await _thisRepository.DeleteByIdAsync(data.Id);
        if (isDelete)
        {
            restful.Data = isDelete;
            restful.Message = "删除成功";
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Data = isDelete;
            restful.Message = "删除失败";
            return restful;
        }
    }
}

